/**
 * Created by Brant on 2016/12/28.
 */
/**
 * Created by Brant on 2016/12/28.
 */
/* @ngInject */
module.exports = function ($rootScope,$scope,$state,$cookies,registerService,$interval,httpservice) {
    $scope.phoneNum = '';
    $scope.phoneCode = '';
    $scope.phonePwd = '';
    $scope.emailNum = '';
    $scope.emailCode = '';
    $scope.emailPwd = '';
    $scope.picVerify = true;
    $scope.verifyCode = '';
    $scope.checkCellPhoneExistUrl = httpservice.checkCellPhoneExistUrl;
    $scope.sendMessage = {
    disable: false,
    text: '发送验证码'
    };
    $scope.userInfo = {userType: 1, sex: 0};

    $scope.mode='1';

    /*用户Id和pwd的默认缓存时间*/
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);

    var emailRegisttype = "2";

    //获取邮箱验证码
    $scope.emailUrl = httpservice.getEmailRegisterCodeUrl +"?time="+(new Date());
    //点击图片,更新验证码
    $scope.getEmailRegisterCode = function () {
        $scope.emailUrl =  httpservice.getEmailRegisterCodeUrl +"?time="+(new Date().getTime());

    };



    if ($('#phoneAgree').is(':checked')){

    }else {
        $('.error_phoneCode').fadeIn();
        $('.error_phoneCode').children('p').html('请确认阅读并接受用户协议及隐私保护协议');
    }

    //点击获取验证码
    $scope.sendMesCode = function(){

        $('.error_phonePwd').fadeOut();
        $('#phonePwd').removeClass('writeError');
        $('.error_phoneNum').fadeOut();
        $('#phoneNum').removeClass('writeError');
        //判断账号
        if (checkPhone($scope.phoneNum)){
            //判断密码
            if ($scope.phonePwd != ''){
                $scope.time();
                //请求接口
                registerService.sendMesCode($scope.phoneNum,$scope.mode).then(function () {

                });

            }else {
                $('#phonePwd').addClass('writeError');
                $('.error_phonePwd').fadeIn();
            }
        }else {
            $('#phoneNum').addClass('writeError');
            $('.error_phoneNum').fadeIn();
            $('.error_phoneNum').children('p').html("手机号码不正确");
        }

    }

    var wait = 60;
    $scope.time = function (){
        if (wait == 0) {

            $('#getCode').attr("disabled",false);
            $('#getCode').val("获取验证码");
            wait = 60;
        } else {

            $('#getCode').attr("disabled",true);
            $('#getCode').val("重新发送（" + wait + "）");
            wait--;

            setTimeout(function () {
                $scope.time()
            }, 1000)
        }
    }

  //服务条款
  $scope.mtermService=function (id) {
      if(id==1){
        $state.go("termService");
      }else{
        $state.go("privacyStatemant");
      }
  };
    //邮箱注册
    $scope.emailRegister = function(){
        $('#emailNum').removeClass('writeError');
        $('.error_emailNum').fadeOut();
        $('#vcode').removeClass('writeError');

        if ($('#emailAgree').is(':checked')){
            if (CheckMail($scope.emailNum)){

                $('#emailNum').removeClass('writeError');
                $('.error_emailNum').fadeOut();
                $('.error_emailNum').children('p').html("");
                //密码
                if ($scope.emailPwd.length >= 4 && $scope.emailPwd.length <= 20){

                    $('#emailPwd').removeClass('writeError');
                    $('.error_emailNum').fadeOut();
                    $('.error_emailNum').children('p').html("");
                    //验证码
                    if($scope.verifyCode.length == 4){

                        $('#vcode').removeClass('writeError');
                        $('.error_emailCode').fadeOut();
                        $('.error_emailNum').children('p').html("");
                      if($scope.userInfo.cellphone!=''){
                        $('input[name="msgcode"]').removeClass('writeError');
                        $('.error_emailNum').fadeOut();
                        $('.error_emailNum').children('p').html("");
                        $rootScope.loadingShow = true;
                        $rootScope.fadeShow = true;
                        registerService.emailregister($scope.emailNum,$scope.emailPwd,$scope.verifyCode,emailRegisttype,$scope.userInfo.msgCode,$scope.userInfo.cellphone).then(function (data) {
                          $rootScope.loadingShow = false;
                          $rootScope.fadeShow = false;
                          if (data.code == 1){
                            $('.error_emailNum').fadeOut();
                            $('.error_emailNum').children('p').html("");
                            $state.go('loginHeader.emailright');
                            // $state.go('login.emailright');

                            //保存注册的邮箱,下个界面用
                            var personageEmail = kbt.encrypt($scope.emailNum);
                            $cookies.put('personageEmail',personageEmail,{
                              expires:expireDate
                            });
                          }else {
                            $('.error_emailNum').fadeIn();
                            $('.error_emailNum').children('p').html(data.msg);
                            $scope.getEmailRegisterCode();
                          }
                        }, function () {
                          $rootScope.loadingShow = false;
                          $rootScope.fadeShow = false;
                        });
                      }else{
                        $('input[name="msgcode"]').addClass('writeError');
                        $('.error_emailNum').fadeIn();
                        $('.error_emailNum').children('p').html("手机验证码不能为空");
                      }
                    }else {
                        $('#vcode').addClass('writeError');
                        $('.error_emailCode').fadeIn();
                        $('.error_emailNum').children('.prompt').html("验证码错误");
                    }

                }else {
                    $('#emailPwd').addClass('writeError');
                    $('.error_emailNum').fadeIn();
                   // $('.error_emailNum').children('p').html("密码长度小于6位");
                  $('.error_emailNum').children('p').html("密码长度小于6位");
                }

            }else {
                $('#emailNum').addClass('writeError');
                $('.error_emailNum').fadeIn();
                $('.error_emailNum').children('p').html("邮件格式错误");
            }
        }else {
            $('.error_emailNum').fadeIn();
            $('.error_emailNum').children('p').html("请确认阅读并接受用户协议及隐私保护协议");
        }
    };
    //
    // /**
    //  * 查询是否绑定过手机
    //  */
    // $scope.getRegisterPhone = function () {
    //     if ($scope.phoneNum != "") {
    //         if (checkPhone($scope.phoneNum)){
    //
    //             registerService.getRegisterPhone($scope.phoneNum).then(function (response) {
    //                 if (response.code == 0) {
    //                     $('#phoneNum').addClass('writeError');
    //                     $('.error_phoneNum').fadeIn();
    //                     $('.error_phoneNum').children('p').html(response.msg);
    //                 }
    //             }, function (response) {
    //                 console.error('getBindPhone error', response.status, response.statusText);
    //             });
    //
    //         }else {
    //             $('#phoneNum').addClass('writeError');
    //             $('.error_phoneNum').fadeIn();
    //             $('.error_phoneNum').children('p').html("手机号码不正确");
    //         }
    //     }
    // };

  /*验证手机*/
  $scope.sendMessage.disable = true;
  $scope.checkPhoneNum=function(){
    var cellPhone=/^1(3|4|5|7|8)[0-9]\d{8}$/;
    if($scope.userInfo.cellphone!=''&&$scope.userInfo.cellphone!=undefined){
      $('input[name="phonenum"]').removeClass('writeError');
      $('.error_emailNum').fadeOut();
      $('.error_emailNum').children('p').html("");
      if($scope.userInfo.cellphone.match(cellPhone)){
        $scope.sendMessage.disable = false;
        $('input[name="phonenum"]').removeClass('writeError');
        $('.error_emailNum').fadeOut();
        $('.error_emailNum').children('p').html("");
      }else{
        $scope.sendMessage.disable = true;
        $('input[name="phonenum"]').addClass('writeError');
        $('.error_emailNum ').fadeIn();
        $('.error_emailNum').children('p').html("请输入正确的手机号");
      }
    }else{
      $scope.sendMessage.disable = true;
      $('input[name="phonenum"]').addClass('writeError');
      $('.error_emailNum ').fadeIn();
      $('.error_emailNum').children('p').html("手机号不能为空");
    }

  };


    /**
     * 查询是否绑定过邮箱
     */
    $scope.getRegisterEmail = function () {
        if ($scope.emailNum != "") {
            $('#emailNum').removeClass('writeError');
            $('.error_emailNum').fadeOut();
            $('.error_emailNum').children('p').html("");
            if (CheckMail($scope.emailNum)) {
                registerService.getRegisterEmail($scope.emailNum).then(function (response) {
                    if (response.code == 0) {
                        $('#emailNum').addClass('writeError');
                        $('.error_emailNum').fadeIn();
                        $('.error_emailNum').children('p').html(response.msg);
                    } else {
                        $('#emailNum').removeClass('writeError');
                        $('.error_emailNum').fadeOut();
                        $('.error_emailNum').children('p').html("");
                    }
                }, function (response) {
                    console.error('getBindPhone error', response.status, response.statusText);
                });
            }else {
                $('#emailNum').addClass('writeError');
                $('.error_emailNum').fadeIn();
                $('.error_emailNum').children('p').html("邮箱不正确");
            }
        }else {
            $('#emailNum').addClass('writeError');
            $('.error_emailNum').fadeIn();
            $('.error_emailNum').children('p').html("邮箱不正确");
        }
    };

    /**
     * 查询密码长度
     */
    $scope.checkPassword = function () {
        if (!($scope.emailPwd.length >= 6 && $scope.emailPwd.length <=20)) {
            $('#emailPwd').addClass('writeError');
            $('.error_emailNum').fadeIn();
            $('.error_emailNum').children('p').html("密码应为6到20位");
            return;
        }
        // else {
        // }
        if(!checkPassword($scope.emailPwd)) {
          $('#emailPwd').addClass('writeError');
          $('.error_emailNum').fadeIn();
          $('.error_emailNum').children('p').html("密码应是6-20位，数字+字母");
          return;
        }
        $('#emailPwd').removeClass('writeError');
        $('.error_emailNum').fadeOut();
        $('.error_emailNum').children('p').html("");
    };

    /**
     * 查询验证码长度
     */
    $scope.sendMessage.disableT = true;
    $scope.checkVerifyCode = function () {
      if ($scope.verifyCode.length == 4) {
        $('#vcode').removeClass('writeError');
        $('.error_emailNum').fadeOut();
        $('.error_emailNum').children('p').html("");
        $scope.sendMessage.disableT = false;
        if($scope.userInfo.cellphone!=''&&$scope.userInfo.cellphone!=undefined){
          $scope.sendMessage.disable = false;
        }else{
          $scope.sendMessage.disable = true;
          $('input[name="phonenum"]').addClass('writeError');
          $('.error_emailNum ').fadeIn();
          $('.error_emailNum').children('p').html("手机号不能为空");
        }

      }else {
        $('#vcode').addClass('writeError');
        $('.error_emailNum').fadeIn();
        $('.error_emailNum').children('p').html("验证码应为4位");
        $scope.sendMessage.disableT = true;
      }
    };
    /*手机验证码*/
  $scope.checkMsgCode = function () {
    if ($scope.userInfo.msgCode!=undefined) {
      if($scope.userInfo.msgCode.length==6){
        $('input[name="msgcode"]').removeClass('writeError');
        $('.error_emailNum').fadeOut();
        $('.error_emailNum').children('p').html("");
      }else{
        $('input[name="msgcode"]').addClass('writeError');
        $('.error_emailNum').fadeIn();
        $('.error_emailNum').children('p').html("手机验证码不能少于6位");
      }
    }else {
      $('input[name="msgcode"]').addClass('writeError');
      $('.error_emailNum').fadeIn();
      $('.error_emailNum').children('p').html("手机验证码不能为空");
    }
  };


    /*发送验证发*/
  $scope.sendCode=function() {
    var cellPhone=/^1(3|4|5|7|8)[0-9]\d{8}$/;
    if ($scope.userInfo.cellphone!=''&&$scope.userInfo.cellphone.match(cellPhone)) {
      $scope.sendMessage.disable = true;
      //发送
      registerService.sendCode($scope.userInfo.cellphone,$scope.verifyCode).then(function (response) {
        if (response.data.code == 1) {
          //发送成功，开始倒计时
          var times = 60;
          $scope.sendBtnDisable = true;
          var coutdown = $interval(function () {
            if (times > 1) {
              times--;
              $scope.sendMessage.disable = true;
              $scope.sendMessage.text = " 重新发送 (" + times + "S) ";
            } else {
              $interval.cancel(coutdown);
              $scope.sendMessage.disable = false;
              $scope.sendMessage.text = " 重新发送 ";
            }
          }, 1000);
        } else {
          $scope.sendMessage.disable = true;
          $('#vcode').addClass('writeError');
          $('.error_emailNum').fadeIn();
          $('.error_emailNum').children('p').html(response.data.msg);
        }
      })
    } else {
      $scope.sendMessage.disable = false;
    }
  }


  $scope.changePwdCode = function(){
    if($scope.emailPwd!==""){
      if ($("#emailPwd").attr("type") == "password") {
        $("#eye").attr("src", "../assets/img/eye-on.svg");
        $("#emailPwd").attr("type", "text")
      }
      else {
        $("#eye").attr("src", "../assets/img/eye-off.svg");
        $("#emailPwd").attr("type", "password")
      }

    }else{

    }

  }


};
